/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.error;

import com.wttech.dzzf.process.dao.DataProcessFinalDao;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 填充绑定账户类型信息
 *
 * @author MaShibo
 * @date 2017/12/4.
 */
@Service
public class FillBindAccountType {

	private static final String FILE_PATH = "F:\\Users\\MaShibo\\Desktop\\BindAccountType.txt";

	private static final Logger LOGGER = LoggerFactory.getLogger(FillBindAccountType.class);

	@Resource
	private DataProcessFinalDao dataProcessFinalDao;

	@Resource
	private BaseInfoService baseInfoService;

	@Resource
	private ThreadPoolTaskExecutor executor;

	private void process() {
		List<Future<Void>> futures = new ArrayList<>();
		try (BufferedReader bf = new BufferedReader(new FileReader(FILE_PATH))) {
			bf.lines().forEach(line -> futures.add(executor.submit(() -> processTextLine(line))));
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		baseInfoService.getFutureResult(futures);
		LOGGER.info("执行完毕");
		executor.shutdown();
	}

	private Void processTextLine(String line) {
		try {
			String[] entries = line.split("\t");
			String ecardNo = entries[0];
			String bindAccountType = StringUtils.trim(entries[1]);
			LOGGER.info("卡号【{}】， 绑定账户类型【{}】", ecardNo, bindAccountType);
			int num = dataProcessFinalDao.updateCardBindAccountType(Integer.parseInt(bindAccountType), StringUtils.substring(ecardNo, 4));
			if (LOGGER.isInfoEnabled()){
				LOGGER.info("更新{}", num > 0 ? "成功" : "失败");
			}
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return null;
	}

	private Void processTextLineGD(String line){
		try {
			String[] entries = line.split("\t");
			String ecardNo = StringUtils.trim(entries[0]).substring(4);
			int cardBrand = Integer.parseInt(StringUtils.trim(entries[1]));
			int  bindAccountType = Integer.parseInt(StringUtils.trim(entries[3]));
			int bindCardType = Integer.parseInt(StringUtils.trim(entries[4]));
			dataProcessFinalDao.updateCardAboutInfos(cardBrand, bindAccountType, bindCardType, ecardNo);
			LOGGER.info("更新成功！");
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return null;
	}


	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		FillBindAccountType service = context.getBean(FillBindAccountType.class);
		service.process();
	}
}
